package co.fitstart.coach.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.collections.MapUtils;
import org.springframework.stereotype.Service;

import com.demo2do.core.persistence.annotation.Dynamic;
import com.demo2do.core.utils.DateFormatUtils;

import co.fitstart.coach.dao.CourseDao;
import co.fitstart.coach.dao.RecordDao;
import co.fitstart.coach.service.RecordService;
import co.fitstart.entity.course.round.CourseRound;
import co.fitstart.entity.course.schedule.CourseSchedule;
import co.fitstart.entity.user.StudentTargetData;
import co.fitstart.entity.user.bwh.BwhRecord;
import co.fitstart.entity.user.inbody.InbodyRecord;

/**
 * 
 * @author wangliwang
 *
 */
@Service("recordService")
public class RecordServiceImpl implements RecordService {
    
    @Dynamic
    private RecordDao recordDao;
    
    @Dynamic
    private CourseDao courseDao;
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.RecordService#listInbodyRecoreds(co.fitstart.entity.course.round.CourseRound)
     */
    public List<InbodyRecord> listInbodyRecoreds(CourseRound courseRound) {
        
        List<InbodyRecord> inbodyRecords = new ArrayList<InbodyRecord>();
        
        Map<String, String> times = this.getTimes(courseRound);
        if(MapUtils.isNotEmpty(times)) {
            inbodyRecords = recordDao.listInbodyRecord(courseRound.getStudent().getUser(), times.get("startTime"), times.get("endTime"));
        }
        
        return inbodyRecords;
        
    }
    
    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.RecordService#listBwhRecords(co.fitstart.entity.course.round.CourseRound)
     */
    public List<BwhRecord> listBwhRecords(CourseRound courseRound) {
        
        List<BwhRecord> bwhRecords = new ArrayList<BwhRecord>();
        
        Map<String, String> times = this.getTimes(courseRound);
        if(MapUtils.isNotEmpty(times)) {
            bwhRecords = recordDao.listBwhRecord(courseRound.getStudent().getUser(), times.get("startTime"), times.get("endTime"));
        }
        
        return bwhRecords;
        
    }
    
    /**
     * 
     * @param courseRound
     * @return
     */
    private Map<String, String> getTimes(CourseRound courseRound) {
        Map<String, String> times = new HashMap<String, String>();
        
        List<CourseSchedule> courseSchedules = courseDao.listCourseSchedule(courseRound);
        if(!courseSchedules.isEmpty()) { 
            times.put("startTime", DateFormatUtils.format(courseSchedules.get(courseSchedules.size()-1).getOccurDate(), "yyyy-MM-dd") + " 00:00:00");
            times.put("endTime", DateFormatUtils.format(courseSchedules.get(0).getOccurDate(), "yyyy-MM-dd") + " 23:23:59");
        }
        
        return times;
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.RecordService#getTarget(co.fitstart.entity.course.round.CourseRound)
     */
    public List<StudentTargetData> listTargets(CourseRound courseRound) {
        return recordDao.listTargets(courseRound);
    }

    /*
     * (non-Javadoc)
     * @see co.fitstart.coach.service.RecordService#loadStudentTargetData(co.fitstart.entity.course.round.CourseRound)
     */
    public StudentTargetData loadStudentTargetData(CourseRound courseRound) {
        return recordDao.loadStudentTargetData(courseRound);
    }
}
